package stacks;

import java.util.Stack;

/**
 * Created by Tzachi on 11/10/2014.
 */
public class HanoiTowers {


    public static void main(String[] args) {
        Tower[] towers = new Tower[3];
        for (int i = 0; i < 3; i++) towers[i] = new Tower();
        int n = 4;
        for (int i = n - 1; i >= 0; i--) towers[0].addDisk(i);
        towers[0].moveDisks(n, towers[1], towers[2]);
        System.out.println();
    }

    public static class Tower {
        Stack<Integer> stack = new Stack<Integer>();

        public void moveDisks(int numberOfDisks, Tower bufferTower, Tower targetTower) {
            if (numberOfDisks == 2) {
                bufferTower.addDisk(removeDisk());
                targetTower.addDisk(removeDisk());
                targetTower.addDisk(bufferTower.removeDisk());
            } else {
                moveDisks(numberOfDisks - 1, targetTower, bufferTower);
                targetTower.addDisk(removeDisk());
                bufferTower.moveDisks(numberOfDisks - 1, this, targetTower);
            }
        }

        public void addDisk(int n) {
            this.stack.push(n);
        }

        public int removeDisk() {
            return stack.pop();
        }
    }
}
